
function PTooltips(){
	this.init.apply(this,arguments)
}
function PTooltipsEventRegister(rule,proxy){
	var regCat=new RegExp(rule,"i");
	jq("a").each(function(){
		if(regCat.test(jq(this).attr('href'))){
			new PTooltips(this,proxy)
		}
	})
}
PTooltips.prototype={
	"handle":null,"object":null,"option":null,"container":"tooltip","event":null,"isLocked":false,"mirror":null,"loadLocked":false,"proxy":null,"gXCoord":null,"gYCoord":null,"init":function(obj,proxy){
		if(obj==null)alert("Cannot found object!");
		if(proxy==null)alert("Cannot found proxy!");
		if(obj&&proxy){
			this.object=obj;
			this.proxy=proxy;
			this.eventRegist()
		}
	}
	,"showRegistObject":function(){
		var putId=(new Date()).getTime()+Math.round(Math.random()*100);
		var thisObject=this;
		var proxyUrl;
		if(this.proxy.lastIndexOf("?")>-1)proxyUrl=this.proxy+"&";
		else proxyUrl=this.proxy+"?";
		this.loadLocked=true;
		jq.ajax({
			url:proxyUrl+"request="+encodeURIComponent(jq(thisObject.object).attr("href")),dataType:"jsonp",success:function(data){
				thisObject.option=data;
				if(PTooltips.handle==thisObject){
					thisObject.showObject()
				}
			}
		})
	}
	,"showObject":function(){
		if(this.option==null&&!this.loadLocked){
			this.showRegistObject()
		}
		else{
			if(!this.option.data)return;
			var tooltip=jq("#"+this.container);
			if(this.mirror){
				tooltip.html(this.mirror.html())
			}
			else{
				var mixer=tooltip.clone();
				mixer.html("<div part='icon' style='float:left;'></div><div part='tips' style='float:left;'></div>");
				if(this.option.icon){
					mixer.find("div[part=icon]").css({
						"background-image":"url('"+this.option.icon+"')","width":this.option.iconWidth?this.option.iconWidth:"0px","height":this.option.iconHeight?this.option.iconHeight:"0px","margin-right":"5px"
					});
					mixer.find("div[part=icon]").show()
				}
				else mixer.find("div[part=icon]").hide();
				mixer.find("div[part=tips]").html(this.option.data);
				mixer=this.initCss(mixer);
				if(this.option.css){
					for(var i in this.option.css){
						mixer.find(i).css(this.option.css[i])
					}
				}
				this.mirror=mixer;
				tooltip.html(mixer.html())
			}
			this.adjustPosition();
			tooltip.show()
		}
		this.isLocked=false
	}
	,"hideObject":function(){
		jq("#"+this.container).hide()
	}
	,"initCss":function(mixer){
		if(this.option.width)mixer.find("div[part=tips]").css({
			"width":this.option.width
		});
		if(this.option.height)mixer.find("div[part=tips]").css({
			"height":this.option.height
		});
		if(this.option.defaultStyle){
			mixer.find("div[part=icon]").css({
				"opacity":"0.9","border":" 1px solid #353837","-moz-border-radius":"5px","-webkit-border-radius":"5px","border-radius":"5px","-moz-box-shadow":"0 0 10px #000","-webkit-box-shadow":"0 0 10px black","box-shadow":"0 0 10px black"
			});
			mixer.find("div[part=tips]").css({
				"_width":"355px","background":"#000","padding":"10px","border":"1px solid #353837","opacity":"0.9","color":"#FFFFFF","max-width":"355px","-moz-border-radius":"2px","-webkit-border-radius":"2px","border-radius":"2px","-moz-box-shadow":"0 0 10px #000","-webkit-box-shadow":"0 0 10px black","box-shadow":"0 0 10px black","border-radius":"5px 5px 5px 5px"
			});
		}
		return mixer;
	}
	,"adjustPosition":function(){
		var tooltip=jq("#"+this.container);
		var windowSize=this.getWindowSize();
		var scrollPos=this.getScrollPos();
		var offsetWidth=tooltip[0].offsetWidth;
		var offsetHeight=tooltip[0].offsetHeight;
		var gLeftEdgeOffset=-15;
		var gTopEdgeOffset=15;
		if(window.innerHeight){
			this.gXCoord=this.event.pageX;
			this.gYCoord=this.event.pageY
		}
		else{
			this.gXCoord=this.event.clientX+scrollPos.x;
			this.gYCoord=this.event.clientY+scrollPos.y
		}
		var x=this.gXCoord+gLeftEdgeOffset;
		var y=this.gYCoord+gTopEdgeOffset;
		if(x+gLeftEdgeOffset+offsetWidth+4>=scrollPos.x+windowSize.w){
			var tempX=this.gXCoord-offsetWidth-gLeftEdgeOffset;
			if(tempX>=0){
				x=tempX
			}
			else{
				x=scrollPos.x+windowSize.w-offsetWidth-gLeftEdgeOffset-4
			}
		}
		if(scrollPos.y+windowSize.h<=y+offsetHeight){
			y=this.gYCoord-offsetHeight-gTopEdgeOffset;
			if(this.option.icon){
				if(this.gXCoord>=x-48&&this.gXCoord<=x&&this.gYCoord>=y-4&&this.gYCoord<=y+48){
					y-=48-(this.gYCoord-y)
				}
			}
		}
		jq("#"+this.container).css({
			left:x+"px",top:y+"px"
		})
	}
	,"eventRegist":function(){
		var thisObj=this;
		jq(this.object).mouseover(function(e){
			PTooltips.handle=thisObj;
			thisObj.event=e;
			thisObj.showObject();
			jq(this).mousemove(function(e){
				PTooltips.handle=thisObj;
				thisObj.event=e;
				thisObj.adjustPosition()
			});
			return false
		});
		jq(this.object).mouseout(function(e){
			PTooltips.handle=null;
			thisObj.event=e;
			jq(this).unbind('mousemove');
			thisObj.hideObject();
			return false
		})
	}
	,"getWindowSize":function(){
		var width=0,height=0;
		if(typeof window.innerWidth=="number"){
			width=window.innerWidth;
			height=window.innerHeight
		}
		else{
			if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)){
				width=document.documentElement.clientWidth;
				height=document.documentElement.clientHeight
			}
			else{
				if(document.body&&(document.body.clientWidth||document.body.clientHeight)){
					width=document.body.clientWidth;
					height=document.body.clientHeight
				}
			}
		}
		return{
			w:width,h:height
		}
	}
	,"getScrollPos":function(){
		var hScroll=0,vScroll=0;
		if(typeof window.pageYOffset=="number"){
			hScroll=window.pageXOffset;
			vScroll=window.pageYOffset
		}
		else{
			if(document.documentElement&&(document.documentElement.scrollLeft||document.documentElement.scrollTop)){
				hScroll=document.documentElement.scrollLeft;
				vScroll=document.documentElement.scrollTop
			}
			else{
				if(document.body&&(document.body.scrollLeft||document.body.scrollTop)){
					hScroll=document.body.scrollLeft;
					vScroll=document.body.scrollTop
				}
			}
		}
		return{
			x:hScroll,y:vScroll
		}
	}
};
jq(function(){
	jq("<div id='tooltip' style='display:none;z-index: 100000001;left:-3360px;top:-3360px;position:absolute;text-align:left;'><div part='icon' style='float:left;'></div><div part='tips' style='float:left;'></div></div>").prependTo("body");
	PTooltipsEventRegister("wow/[a-zA-Z]+[?id=]+[0-9]","http://db.arpggame.com/ajax?mod=wow");
});